<template>
  <el-dialog class="carDialog" :visible.sync="visible" :title="dataForm.id?'编辑':'新增'" width="600px" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form size="small" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="submit()" label-width="160px">
      <el-form-item label="车型" prop="carModel">
        <el-input class="s_input" v-model="dataForm.carModel"></el-input>
      </el-form-item>
      <el-form-item label="厂商指导价" prop="guidePrice">
        <el-input class="s_input" v-model="dataForm.guidePrice"></el-input>
      </el-form-item>
      <el-form-item label="厂商名称" prop="manufacturer">
        <el-input class="s_input" v-model="dataForm.manufacturer"></el-input>
      </el-form-item>
      <el-form-item label="级别" prop="level">
        <el-input class="s_input" v-model="dataForm.level"></el-input>
      </el-form-item>
      <el-form-item label="能源类型">
        <el-input class="s_input" v-model="dataForm.energyType"></el-input>
      </el-form-item>
      <el-form-item label="上市时间" prop="timeMarket">
        <el-input class="s_input" v-model="dataForm.timeMarket"></el-input>
      </el-form-item>
      <el-form-item label="发动机">
        <el-input class="s_input" v-model="dataForm.engine"></el-input>
      </el-form-item>
      <el-form-item label="最大功率" prop="maxPower">
        <el-input class="s_input" v-model.number="dataForm.maxPower"></el-input>
      </el-form-item>
      <el-form-item label="最大扭矩" prop="maxTorque">
        <el-input class="s_input" v-model.number="dataForm.maxTorque"></el-input>
      </el-form-item>
      <el-form-item label="变速箱">
        <el-input class="s_input" v-model="dataForm.transmissionCase"></el-input>
      </el-form-item>
      <el-form-item label="长度" prop="length">
        <el-input class="s_input" v-model.number="dataForm.length"></el-input>
      </el-form-item>
      <el-form-item label="宽度" prop="wide">
        <el-input class="s_input" v-model.number="dataForm.wide"></el-input>
      </el-form-item>
      <el-form-item label="高度" prop="height">
        <el-input class="s_input" v-model.number="dataForm.height"></el-input>
      </el-form-item>
      <el-form-item label="车身结构" prop="carStructure">
        <el-input class="s_input" v-model="dataForm.carStructure"></el-input>
      </el-form-item>
      <el-form-item label="最高车速" prop="maxSpeed">
        <el-input class="s_input" v-model.number="dataForm.maxSpeed"></el-input>
      </el-form-item>
      <el-form-item label="官方百公里加速时间(s)" prop="accelerationTime">
        <el-input class="s_input" v-model.number="dataForm.accelerationTime"></el-input>
      </el-form-item>
      <el-form-item label="车辆库存" prop="stock">
        <el-input class="s_input" v-model.number="dataForm.stock"></el-input>
      </el-form-item>
      <el-form-item label="车辆图片">
        <el-upload
          ref="pictureUpload"
          :action="uploadUrl"
          :headers="headers"
          list-type="picture-card"
          :file-list="dataForm.picture"
          accept=".jpeg,.png,.jpg,.gif"
          :on-success="successPictureHandle"
          :on-remove="removePictureHandle">
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button size="small" @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button size="small" type="primary" @click="submit()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import Cookies from 'js-cookie'
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      uploadUrl: window.SITE_CONFIG['apiURL'] + '/uploadfile',
      headers: { token: Cookies.get('token') },
      dataForm: {
        id: null,
        carModel: null,
        guidePrice: null,
        manufacturer: null,
        level: null,
        energyType: null,
        timeMarket: null,
        engine: null,
        maxTorque: null,
        transmissionCase: null,
        length: null,
        wide: null,
        height: null,
        carStructure: null,
        maxSpeed: null,
        accelerationTime: null,
        stock: null,
        picture: []
      }
    }
  },
  computed: {
    dataRule () {
      return {
        carModel: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        guidePrice: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        manufacturer: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        level: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        timeMarket: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        maxPower: [
          { type: 'number', message: '该选项必须为数字值', trigger: 'blur' }
        ],
        maxTorque: [
          { type: 'number', message: '该选项必须为数字值', trigger: 'blur' }
        ],
        length: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { type: 'number', message: '该选项必须为数字值', trigger: 'blur' }
        ],
        wide: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { type: 'number', message: '该选项必须为数字值', trigger: 'blur' }
        ],
        height: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { type: 'number', message: '该选项必须为数字值', trigger: 'blur' }
        ],
        carStructure: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        maxSpeed: [
          { type: 'number', message: '该选项必须为数字值', trigger: 'blur' }
        ],
        accelerationTime: [
          { type: 'number', message: '该选项必须为数字值', trigger: 'blur' }
        ],
        stock: [
          { type: 'number', message: '该选项必须为数字值', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 显示弹窗
    init (id) {
      this.visible = true
      this.$nextTick(() => {
        console.log(this.$refs.dataForm)
        this.$refs.dataForm.resetFields()
        if (id) {
          this.getInfo(id)
        } else {
          this.resetForm()
        }
      })
    },
    // 图片上传成功
    successPictureHandle (response, file, fileList) {
      console.log(response, file, fileList)
      this.dataForm.picture.push(response.pictureInfoVOS[0])
      this.dataForm.picture = fileList.map(item => {
        if (item.response) {
          return {
            name: item.response.pictureInfoVOS[0].imgName,
            url: item.response.pictureInfoVOS[0].imgAddress
          }
        } else {
          return item
        }
      })
    },
    // 删除图片
    removePictureHandle (file, fileList) {
      console.log(file, fileList)
      this.dataForm.picture = fileList
    },
    resetForm () {
      this.dataForm = {
        id: null,
        carModel: null,
        guidePrice: null,
        manufacturer: null,
        level: null,
        energyType: null,
        timeMarket: null,
        engine: null,
        maxTorque: null,
        transmissionCase: null,
        length: null,
        wide: null,
        height: null,
        carStructure: null,
        maxSpeed: null,
        accelerationTime: null,
        stock: null,
        picture: []
      }
    },
    // 表单提交
    submit: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        let dataForm = JSON.parse(JSON.stringify(this.dataForm))
        dataForm.picture = dataForm.picture.map(item => item.url)
        this.$http[this.dataForm.id ? 'put' : 'post']('/carinfo', dataForm).then(({ data: res }) => {
          if (res.code === 0) {
            this.$message.success('保存成功!')
            this.visible = false
            this.$emit('refresh')
          } else {
            this.$message.error(res.msg)
          }
        }).catch((error) => {
          return this.$message.error(error)
        })
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    // 获取车辆详情信息
    getInfo (id) {
      this.$http.get('/carinfo/info/' + id).then(({ data: res }) => {
        this.dataForm = res.carInfo
        this.dataForm.picture = this.dataForm.picture === null ? [] : this.dataForm.picture.map(item => {
          return {
            url: item,
            name: item
          }
        })
        console.log(this.dataForm)
      })
    }
  }
}
</script>
<style lang="scss">
.carDialog{
  .el-dialog__header{
    .el-dialog__title{
      font-size: 14px;
      font-weight: bold;
    }
    border-bottom: 1px solid #f2f2f2;
  }
  .el-dialog__body{
    padding: 15px 20px;
  }
  .s_input{
    width: 80%;
    margin-right: 10px;
  }
  .s_picker{
    width: 37%;
  }
}
</style>
